package com.yunxie.novelservice.infra.basic.utils;


import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * 数据库加密util
 */
public class DruidEncryptUtils {
    private static String publicKey;
    private static String privateKey;
    static {
        try {
            String[] keypair = ConfigTools.genKeyPair(512);
            privateKey = keypair[0];
            System.out.println("privateKey:"+privateKey);
            publicKey  = keypair[1];
            System.out.println("publicKey:"+publicKey);
        }catch (NoSuchAlgorithmException e){
            e.printStackTrace();
        }catch (NoSuchProviderException e){
            e.printStackTrace();
        }
    }

        public static String encrypt(String plainText) throws Exception {
            String encrypt = ConfigTools.encrypt(privateKey, plainText);
            System.out.println("encrypt:"+encrypt);
            return encrypt;
        }
    public static String decrypt(String encryptText) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey,encryptText);
        System.out.println("encrypt:"+decrypt);
        return decrypt;

    }

    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("mezhateMysql123#E");
        System.out.println("encrypt:"+encrypt);
    }

}
